<?php
namespace app\admin\controller;
use think\Db;
class Goodspackage extends Base
{
    //列表
    public function lists()
    {
        if(is_post()){
            $cur_page     = input('page');
            $keyword  = input('keyword');
            $status  = input('status');
            $min_time = input('min_time');
            $max_time = input('max_time');
            $sql = "
            select
                [1]
            from
                goods_package
            ";
            $param = [];
            $w = ['where'];
            if(!empty($min_time)){
                $w[] = ' and create_time>?';
                $param[] = strtotime($min_time);
            }
            if(!empty($max_time)){
                $w[] = ' and create_time<?';
                $param[] = strtotime($max_time);
            }
            if(!empty($keyword)){
                $w[] = ' and title like ?';
                $param[] = "%{$keyword}%";
            }
            if($status!='100all'){
                $w[] = ' and status = ?';
                $param[] = $status;
            }
            #条件判断
            if (count($w)>1){
              $w[1] = str_replace("and","",$w[1]);
              $sql .= implode(" ",$w);
            }
            #分页
            $page_size = c('a.page_size');
            $page_html = com_list_page($sql, $param, $cur_page,$page_size);

            #内容
            $start = ($cur_page - 1) * $page_size;
            $field = "*";
            $sql_list = str_replace("[1]",$field,$sql);
            $sql_list .= " order by sort desc limit {$start},{$page_size}";
            $rslist = Db::query($sql_list, $param);
            foreach ($rslist as $k => $v) {
                $rslist[$k]['create_time'] = com_formate_time($v['create_time']);
                $rslist[$k]['checkbox'] = com_checkbox($v['id']);
                $rslist[$k]['sort']   = com_sort('goods_package',$v['id'],$v['sort']);
                $rslist[$k]['status'] = c('type.status',$v['status']);
                $rslist[$k]['action']  = com_btn_edit("/goodspackage/edit/id/{$v['id']}");
                $rslist[$k]['action']  .= com_btn_del("/goodspackage/delete/id/{$v['id']}", "您确定要删除【{$v['title']}】？");
            }
            $body_html = com_list_body($rslist, ['checkbox','title','price','sort','status','create_time','action']);
            com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
        }
        return view('',['status'=>c('type.status'),]);
    }
    #添加
    function add(){
      if(is_post()){
            $d = post();
            $rule = [
                'title'=>  'require',
                'goods_ids'=>  'require',
                'price'=>  'require|number',
            ];
            $msg = [
              'title.require'   =>  '请输入套餐标题',
              'price.require'   =>  '请输入套餐价格',
              'goods_ids.require'=>  '请选择产品',
              'price.number'   =>  '请输入数字',
            ];
            com_validate($rule, $msg, $d);
            //验证产品数量
            foreach($d['goods_ids'] as $k => $v){
                $v = (int)$v;
                if($v==0){
                    com_error('产品数量填写错误!');
                    com_check();
                }else{
                  $d['goods_ids'][$k] = $v;
                }
            }
            $d['goods_ids'] = json_encode($d['goods_ids']);
            $d['create_time'] = time();
            $d['update_time'] = time();
            Db::name("goods_package")->insert($d);
            com_check(['suc_msg'=>'添加成功!','suc_js'=>'alert|close|reload']);
      }
      $all= [
          '100all' => [
              'id' => '100all',
              'parent_id' => 0,
              'cat_title' => '全部分类',
              'depath' => 0,
              ]
      ];
      $catlist = $all + \app\admin\model\GoodsCat::allSubCat();
      $rec = ['100all'=>'全部推荐'] + Db::name("goods_rec")->where("status",0)->order("sort desc,id desc")->column('title','id');
      return view('',['status'=>c('type.status'),'catlist'=>$catlist,'rec'=>$rec]);
    }
    #编辑
    function edit($id){
      if(is_post()){
            $d = post();
            $rule = [
                'title'=>  'require',
                'goods_ids'=>  'require',
                'price'=>  'require|number',
            ];
            $msg = [
              'title.require'   =>  '请输入套餐标题',
              'price.require'   =>  '请输入套餐价格',
              'goods_ids.require'=>  '请选择产品',
              'price.number'   =>  '请输入数字',
            ];
            com_validate($rule, $msg, $d);
            //验证产品数量
            foreach($d['goods_ids'] as $k => $v){
                $v = (int)$v;
                if($v==0){
                    com_error('产品数量填写错误!');
                    com_check();
                }else{
                  $d['goods_ids'][$k] = $v;
                }
            }
            $d['goods_ids'] = json_encode($d['goods_ids']);
            $d['update_time'] = time();
            Db::name("goods_package")->update($d);
            com_check(['suc_msg'=>'修改成功!','suc_js'=>'alert|close|reload']);
      }
      $info = Db::name('goods_package')->find($id);
      $goods_ids = json_decode($info['goods_ids'],true);

      $goods = Db::name('goods')->where('id','in',array_keys($goods_ids))->column('title','id');
      $all= [
          '100all' => [
              'id' => '100all',
              'parent_id' => 0,
              'cat_title' => '全部分类',
              'depath' => 0,
              ]
      ];
      $catlist = $all + \app\admin\model\GoodsCat::allSubCat();
      $rec = ['100all'=>'全部推荐'] + Db::name("goods_rec")->where("status",0)->order("sort desc,id desc")->column('title','id');
      return view('',['info'=>$info,'status'=>c('type.status'),'goods'=>$goods,'goods_ids'=>$goods_ids,'catlist'=>$catlist,'rec'=>$rec]);
    }
    #删除
    function delete($id=''){
        Db::name('goods_package')->where('id','in',[$id])->delete();
        com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
    }

    #批量删除
    function delall(){
      if(is_post()){
          $ids = input('ids/a');
          Db::name('goods_package')->where('id','in',$ids)->delete();
          com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
      }
    }

}
